<?php 
/* include the configuration file */
require_once("config.php");

/* include the cache library */
require_once("class/cache.class.php");

/* check if all required parameters exist */
if(array_key_exists("title", $_GET)==true
	&& array_key_exists("ext", $_GET)==true
	&& array_key_exists("w", $_GET)==true){
	// get the requested image width
	$req_width = $_GET["w"];

	// set requested height to -1 by default
	// (no specific height requested)
	$req_height = -1;

	// check if a specific height was requested
	if(array_key_exists("h", $_GET)){
		$req_height = $_GET["h"];
	}
		
	// define the original filename
	$filename = CONFIG_IMAGE_PATH."/"
		.$_GET["title"].".".$_GET["ext"];
		
	// instanciate the cache object
	$c = new cache();
	
	// generate the cache key
	$cache_key = $c->hash("img".$filename."_".$req_width);
	
	// generate the cache file name
	$cached_filename = CONFIG_CACHE_PATH."/".$cache_key.".".$_GET["ext"];
	
	// when the cached file is not older than
	// the original we can use the cache file
	if(file_exists($cached_filename)===false || (filemtime($cached_filename)<(time()+604800))){
		// load the image file
		$img = imagecreatefromstring(file_get_contents($filename));
		
		// get the current width and height
		list($width, $height) = getimagesize($filename);

		// define the source height
		$src_height = 0;
		$src_width = 0;

		// calculate height when not requested
		if($req_height<1){
			$req_height = round(($req_width * $height) / $width);
			$src_height = $height;
			$src_width = $width;
		}else{
			/* we need to calculate the overflow on width or height 
				to ensure the image fills the new height and at the 
				same time keeps its aspect ratio. It will be cut on 
				the y-axis or height respectively */
			$src_height = round($width * ($req_height / $req_width));
			$src_width = $width;

			/* if the image does not have sufficient height, then 
				it needs to be cut on both axis to fit the requested 
				width and height */
			if($src_height > $height){
				$hdiff = $src_height-$height;
				$src_height = $height;
				$src_width = $src_width-$hdiff;
			}
		}

		// resize the image
		$img_resized = imagecreatetruecolor($req_width, $req_height);

		$white = imagecolorallocate($img_resized, 255, 255, 255);
		imagefill($img_resized, 0, 0, $white);
		
		// copy the resampled image
		imagecopyresampled($img_resized, $img, 0, 0, 0, 0, 
					$req_width, $req_height, $src_width, $src_height);
					
		// finally add the file to the cache
		imagejpeg($img_resized,$cached_filename,70);
		
		// finally destroy the image
		imagedestroy($img);
		imagedestroy($img_resized);
	}
	
	// set the expires header to enforce caching
	header("Cache-Control: max-age=604800");
	header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
	header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
	header("Expires: ".gmdate('D, d M Y H:i:s \G\M\T',(time()+604800)));
				
	// output the result
	header("Content-Type: image/jpeg");
	
	// output the image to the browser
	print(file_get_contents($cached_filename));
}
?>
